﻿package utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

import code.NlpirTest;
import utils.*;

public class Proc {
	//获得一个类别的条件概率表。
	public static HashMap<String ,Float> getCPR(String path,int number) throws IOException{
		//path为文件夹路径
		//HashMap<String, HashMap<String, Integer>> allNormalTF =new HashMap<String, HashMap<String, Integer>>();
		//allNormalTF=ReadFile.normalTFAllFiles(path);
		HashMap<String,HashMap<String, Float>> all_tf = ReadFile.tfAllFiles(path);       
        HashMap<String, Float> idfs = ReadFile.idf(all_tf);
        
        HashMap<String, TreeMap<String, Float>> resTfidf=new HashMap<String, TreeMap<String, Float>>();
        resTfidf=ReadFile.tf_idf(all_tf, idfs);
        Display.DisTfIdf(resTfidf);
        
        HashMap<String,HashMap<String,Integer>> selected=new HashMap<String,HashMap<String,Integer>>();
        
        //selected=ReadFile.selTfidf(resTfidf, allNormalTF);  这行语句与其下一行语句等价
        selected=ReadFile.selTfidf(ReadFile.tf_idf(all_tf, idfs), ReadFile.normalTFAllFiles(path),number);
        
        int c1=NBayes.totalWords(selected);       
        //HashMap<String,Integer> cmap=new HashMap<String,Integer>();
        //cmap=NBayes.wordNum(selected);
        HashMap<String,Float> cprMap=new HashMap<String,Float>();
        cprMap=NBayes.wordPr(NBayes.wordNum(selected));
        
        return cprMap;
	}
	//获得待分类文档属于某个类别的概率，传入参数为文件路径和类别文件夹路径
	public static Float classify(String txtpath,String dirc) throws IOException{
		return null;
	}
	
	//获得测试文件夹内文件的路径
	public static String[] getTxtPath(String path){
		List<String> testfile=new ArrayList<String>();
		try
        {
            File file = new File(path);
            if(!file.isDirectory())
            {
                System.out.println("输入的参数应该为[文件夹名]");
                System.out.println("filepath:" + file.getAbsolutePath());
            }
            else
            {
                String[] flist = file.list();
                for(int i = 0; i < flist.length; i++)
                {
                    File newfile = new File(path + "\\" + flist[i]);
                    if(!newfile.isDirectory())
                    {
                    	testfile.add(newfile.getAbsolutePath());
                    }
                    else if(newfile.isDirectory()) //if file is a directory, call ReadDirs
                    {
                    	getTxtPath(path + "\\" + flist[i]);
                    }                    
                }
            }
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
		final int testsize=testfile.size();
		String[] txtpath=(String[])testfile.toArray(new String[testsize]);
		return txtpath;
	}
	
	//获得最大的概率
	public static Float getMax(Float[] pr){
		Float pr_final=(float)0;
		for(int i=0;i<pr.length;i++){
			if(pr[i]>pr_final){
				pr_final=pr[i];
			}
		}
		return pr_final;
	}
	
	//获得待分类文档属于某一个类别的概率，传入参数为待分类文档和条件概率表
	public static Float classify_2(String[] txt,HashMap<String,Float> cprMap) throws FileNotFoundException, IOException{
		Float prlog=(float)0;
		Float pr=(float)1;
		/*
		for(String word:txt){
			if(cprMap.containsKey(word)){			
				pr=(float)Float.parseFloat(cprMap.get(word).toString());
				prlog=prlog+(float)Math.log10(pr);
			}
		}
		*/
		//
		Set txtset=new HashSet();
		for(String word:txt){
			txtset.add(word);
		}
		for(String word:cprMap.keySet()){
			if(txtset.contains(word)){
				pr=(float)Float.parseFloat(cprMap.get(word).toString());
				prlog=prlog+(float)Math.log10(pr);
			}else{
				pr=(float)(1-Float.parseFloat(cprMap.get(word).toString()));
				prlog=prlog+(float)Math.log10(pr);
			}
		}
		//
		
		return Math.abs(prlog);
	}
	
	//

	
	
//**********//	
}
